#include "data.h"
#include "Activity.h"
#include "global.h"


int neh( data md, schedule &ms) {

	deque <Activity> acts;
	int i, j, k, makespan, tmpMakespan;
	deque <int> sumPtimes, nehSched, sched, tmpSched, tmpSched2;
	deque <int>::iterator first, now, it_insert, it_erase ;


	for ( i = 0; i < ms.getnbSched(); i ++ ) {		
		for ( j = 0; j < ms.getnbStages(); j ++ ) {
			Activity act(ms, i, j);
			acts.push_back(act);			
		}		
	}

	for ( i = 0; i <ms.getnbSched(); i ++) {
		int tmp = 0;
		for ( j = 0; j < ms.getnbStages(); j++) {
			tmp += ms.getPtimes( i, j);
		}
		sumPtimes.push_back(tmp);
		sched.push_back(i);

	}

	//descending order:
	for ( i = 1,j; i < ms.getnbSched(); i ++ ) {
		int tmp = sumPtimes[ i ];
		int tmpII = sched[i];
		for ( j = i; j > 0 && tmp > sumPtimes[j - 1] ; j --){
			sumPtimes[j] = sumPtimes[j-1];
			sched[j] = sched[j - 1];
		}
		sumPtimes[j] = tmp;
		sched[j] = tmpII;
	}



	// neh scheduling:
	
	for ( k = 0; k < sched.size() - 1; k ++) {
		tmpSched.push_back(sched[k]);
		for (i  = k+1; i <= tmpSched.size() ; i++ ) {
			makespan = RAND_MAX;
			for ( j = 0; j <= i; j++ ) {
				tmpSched2.clear();
				tmpSched2.assign(tmpSched.begin(), tmpSched.end());
				tmpSched2.insert(tmpSched2.begin() + j, sched[i]);
				tmpMakespan = flowshop (acts, tmpSched2, ms.getnbStages());
				/*
				cout << endl;
				cout << "tmpMakespan " << tmpMakespan << endl;
				cout << "tmpSched2 " << endl;
				
				first = tmpSched2.begin();
				while (first < tmpSched2.end()) {
					cout << *first << " ";
					first++;
				}
				*/

				if ( makespan > tmpMakespan) {
					makespan = tmpMakespan;
					nehSched.clear();
					nehSched.assign(tmpSched2.begin(), tmpSched2.end());
				}


				/*
				cout << endl;
				cout << "neh makespan " << makespan << endl;
				cout << "nehSched " << endl;
				first = nehSched.begin();
				while (first < nehSched.end()) {
					cout << *first << " ";
					first++;
				}
				cout << endl;
				*/
				

			} 
		}
	}


	for( i = 0; i < ms.getnbSched(); i ++ ){
		ms.setSched( i, nehSched[i]);
	}

	return 0;

}